﻿using MiniApiProject.Auth;
using MiniApiProject.Data;
using MiniApiProject.Endpoints.Internal;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// builder.Configuration.AddJsonFile("appsettings.json", true, true);
builder.Services.AddAuthentication(ApiKeySchemeConstants.SchemeName)
    .AddScheme<ApiKeyAuthSchemeOptions, ApiKeyAuthHandler>(ApiKeySchemeConstants.SchemeName, null);
builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IDbConnectionFactory>(new DbConnectionFactory(builder.Configuration.GetSection("ConnectionStrings:BookConnectionString").Value!));
builder.Services.AddSingleton<DbInitializer>();
builder.Services.AddEndpoints<Program>(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
app.Urls.Add("http://localhost:5001");
if (app.Environment.IsDevelopment())
{
	app.UseSwagger();
	app.UseSwaggerUI();
}
app.UseAuthorization();
app.Services.GetService<DbInitializer>()?.Init();

app.UseEndpoints<Program>();

app.Run();
